﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;


public partial class BoatList : System.Web.UI.Page
{
    public string BoatName { get; set; }
    public string SortField { get; set; }
    public string SortOption { get; set; }
    public string BoatId { get; set; }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
            {
                return int.Parse(Request["page"].ToString());
            }
            return 1;
        }
    }
    public int PageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPropertyFromQueryString();
            Search();
        }
    }
    void GetPropertyFromQueryString()
    {
        if (Request["BoatId"] != null)
            BoatId = Request["BoatId"];
        else
            BoatId = string.Empty;
        txtBoatID.Text = BoatId;
        if (Request["BoatName"] != null)
            BoatName = Request["BoatName"].ToString();
        else
            BoatName = string.Empty;
        txtBoatName.Text = BoatName;


        //if (Request["SortField"] == null)
        //    SortField = ddlSortField.SelectedValue;
        //else
        //{
        //    SortField = Request["SortField"].ToString();
        //    ddlSortField.SelectedValue = SortField;
        //}
        //if (Request["SortOption"] == null)
        //    SortOption = ddlOption.SelectedValue;
        //else
        //{
        //    SortOption = Request["SortOption"].ToString();
        //    ddlOption.SelectedValue = SortOption;
        //}

        //PageIndex = Request["Page"] == null ? 1 : int.Parse(Request["Page"].ToString());
    }
    void GetPropertyFromForm()
    {
        BoatName = txtBoatName.Text;
        BoatId = txtBoatID.Text;
    }
    void Search()
    {
        HashSet<string> hs = new HashSet<string>();
        //hs.Add(SortField + " " + SortOption);
        int count = 0;
        grv.DataSource = BoatDAO.CreateInstant().FindBoat(BoatId, BoatName, null, null, true, PageIndex, PageSize, out count, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = count;
        smartPager.UrlFormatString = string.Format(@"/Pages/Boats/BoatList.aspx?BoatName={0}&BoatId={1}&SortField={2}&SortOption={3}", BoatName, BoatId, SortField, SortOption) + "&page={0}";
        smartPager.Visible = count > PageSize;
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        GetPropertyFromForm();
        Response.Redirect(string.Format(@"/Pages/Boats/BoatList.aspx?BoatName={0}&BoatId={1}&SortField={2}&SortOption={3}", BoatName, BoatId, SortField, SortOption));
    }
}
